package jinyilw.swing.visuals;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class ButtonGroupPanel extends JPanel
{
	private static final long serialVersionUID = 8638214369729294253L;
	ButtonGroup buttongroup = new ButtonGroup();
	ArrayList<AbstractButton> list = new ArrayList<>();

	public ButtonGroupPanel()
	{
		// setOpaque(false);
	}

	public void setSelectedIndex(int index)
	{
		if (index > -1 && index < list.size())
		{
			list.get(index).setSelected(true);
		}
	}

	public void add(AbstractButton ab)
	{
		super.add(ab);
		buttongroup.add(ab);
		list.add(ab);
	}
	public void addOther(Component com)
	{
		super.add(com);
	}

	public int getSelectIndex()
	{
		int i;
		for (i = 0; i < list.size(); i++)
			if (list.get(i).isSelected())
				return i;
		return -1;
	}
	public void addActionListener(ActionListener listener)
	{
		for (AbstractButton b : list)
			b.addActionListener(listener);
	}
}
